package com.smartmap;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.andreabaccega.widget.FormEditText;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class RegisterActivity extends BasicActivity {
	private ImageView checkImageView;
	private LinearLayout checkImageLayout;
	private FormEditText emailText, nickNameText, pwdText;
	private ImageButton registerButton;
	private ImageView topImage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		setViews();

	}

	private void setViews() {
		checkImageLayout = (LinearLayout) findViewById(R.id.register_check_layout);
		checkImageView = (ImageView) findViewById(R.id.register_check_view);
		emailText = (FormEditText) findViewById(R.id.register_email);
		nickNameText = (FormEditText) findViewById(R.id.register_nickName);
		pwdText = (FormEditText) findViewById(R.id.register_pwd);
		registerButton = (ImageButton) findViewById(R.id.register_btn);
		topImage = (ImageView) findViewById(R.id.topImage);
		topImage.setBackgroundResource(R.drawable.top_register);
	}

	private void setEvent() {
		checkImageLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (checkImageView.isShown()) {
					checkImageView.setVisibility(View.INVISIBLE);
				} else {
					checkImageView.setVisibility(View.VISIBLE);
				}
			}
		});
		registerButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (emailText.testValidity() && nickNameText.testValidity()
						&& pwdText.testValidity()) {

					regiest(emailText.getText().toString(), nickNameText
							.getText().toString(), pwdText.getEditableText()
							.toString());

				}

			}
		});
	}

	private void store(String email, String nickName, String pwd, String memberId) {
		sharedPreferencesUtils.setNickName(nickName);
		sharedPreferencesUtils.setEmail(email);
		sharedPreferencesUtils.setPwd(pwd);
		sharedPreferencesUtils.setMemberId(memberId);
	}

	@Override
	protected void onResume() {
		super.onResume();
		setEvent();
	}

	private void regiest(final String email, final String nickName,
			final String pwd) {
		AsyncHttpClient client = new AsyncHttpClient();
		Map<String, String> params = new HashMap<String, String>();
		params.put("nickName", nickName);
		client.post("http://172.31.113.147:8080/smartmap/talk/regiest",
				new RequestParams(params), new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String response) {
						store(email, nickName, pwd, response);
						Intent intent = new Intent(RegisterActivity.this,
								LoginActivity.class);
						startActivity(intent);
					}
				});
	}
}
